/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package turnibook.view;

import datechooser.model.multiple.PeriodSet;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import turnibook.utils.Archivo;
import turnibook.utils.CrearPdf;
import turnibook.utils.EnviadorMail;
import turnibook.model.Operaciones;

/**
 *
 * @author julietapoume wrtfix
 * @version 1.0
 */
public class Agenda extends javax.swing.JFrame {

    
    private ArrayList<String> dia;
    /**
     * Creates new form Agenda
     */
    //Cada numero representa la columna de la tabla
    private static int ASISTENCIA = 6;
    private static int DESCRIPCION = 5;
    private static int OS = 4;
    private static int TEL = 3;
    private static int DOC = 2;
    private static int NOMBRE = 1;
    private static int HORA = 0;
    private String id_horario;
    private String id_profesional;
    private String email;
    private int tiempo;

     HashMap<String,Boolean> diasLaborales;

    public void setDiasLaborales(HashMap<String, Boolean> diasLaborales) {
        this.diasLaborales = diasLaborales;
    }
    
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getId_profesional() {
        return id_profesional;
    }

    public void setId_profesional(String id_profesional) {
        this.id_profesional = id_profesional;
    }

    public String getId_horario() {
        return id_horario;
    }

    public void setId_horario(String id_horario) {
        this.id_horario = id_horario;
    }

    public Agenda() {
        initComponents();
        
        //TODO Terminar el coloreo del calendario.        

        
        dia = new ArrayList<String>();
        dia.add("domingo");
        dia.add("lunes");
        dia.add("martes");
        dia.add("miercoles");
        dia.add("jueves");
        dia.add("viernes");
        dia.add("sabado");                

        beliminar.setEnabled(false);
        bcancelar.setEnabled(false);
        bguardar.setEnabled(false);
        Sobreturno.setEnabled(false);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        dateChooserDialog1 = new datechooser.beans.DateChooserDialog();
        jMenu1 = new javax.swing.JMenu();
        dateChooserDialog2 = new datechooser.beans.DateChooserDialog();
        dateChooserDialog3 = new datechooser.beans.DateChooserDialog();
        buttonGroup1 = new javax.swing.ButtonGroup();
        jPopupMenu1 = new javax.swing.JPopupMenu();
        bmodificar = new javax.swing.JButton();
        beliminar = new javax.swing.JButton();
        bguardar = new javax.swing.JButton();
        dateChooserPanel1 = new datechooser.beans.DateChooserPanel();
        calendario = new datechooser.beans.DateChooserPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaAgenda = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        panel1 = new java.awt.Panel();
        Dni = new javax.swing.JCheckBox();
        Nombre = new javax.swing.JCheckBox();
        obraSocial = new javax.swing.JCheckBox();
        buscar1 = new javax.swing.JTextField();
        buscar2 = new javax.swing.JTextField();
        buscar3 = new javax.swing.JTextField();
        botonBuscar = new java.awt.Button();
        jScrollPane2 = new javax.swing.JScrollPane();
        lista = new javax.swing.JList();
        proximo = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        notas = new javax.swing.JTextArea();
        gnota = new javax.swing.JButton();
        bnota = new javax.swing.JButton();
        aviso = new javax.swing.JTextField();
        Sobreturno = new javax.swing.JButton();
        bcancelar = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        jMenuItem3 = new javax.swing.JMenuItem();

        jMenu1.setText("jMenu1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Sistema de Gestión de Turnos");
        setBackground(new java.awt.Color(255, 204, 204));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });
        addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                formMouseMoved(evt);
            }
        });

        bmodificar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/turnibook/images/modificar.png"))); // NOI18N
        bmodificar.setText("Modificar");
        bmodificar.setMaximumSize(new java.awt.Dimension(93, 27));
        bmodificar.setMinimumSize(new java.awt.Dimension(93, 27));
        bmodificar.setPreferredSize(new java.awt.Dimension(89, 25));
        bmodificar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                bmodificarMouseClicked(evt);
            }
        });
        bmodificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bmodificarActionPerformed(evt);
            }
        });

        beliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/turnibook/images/papelera.gif"))); // NOI18N
        beliminar.setText("Eliminar");
        beliminar.setMaximumSize(new java.awt.Dimension(93, 27));
        beliminar.setMinimumSize(new java.awt.Dimension(93, 27));
        beliminar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                beliminarMouseClicked(evt);
            }
        });
        beliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                beliminarActionPerformed(evt);
            }
        });

        bguardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/turnibook/images/icoGuardar.gif"))); // NOI18N
        bguardar.setText("Guardar");
        bguardar.setPreferredSize(new java.awt.Dimension(89, 25));
        bguardar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                bguardarMouseClicked(evt);
            }
        });
        bguardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bguardarActionPerformed(evt);
            }
        });

        calendario.setCurrentView(new datechooser.view.appearance.AppearancesList("custom",
            new datechooser.view.appearance.ViewAppearance("custom",
                new datechooser.view.appearance.swing.SwingCellAppearance(new java.awt.Font("Tahoma", java.awt.Font.PLAIN, 11),
                    new java.awt.Color(0, 0, 0),
                    new java.awt.Color(0, 0, 255),
                    false,
                    true,
                    new datechooser.view.appearance.swing.ButtonPainter()),
                new datechooser.view.appearance.swing.SwingCellAppearance(new java.awt.Font("Tahoma", java.awt.Font.PLAIN, 11),
                    new java.awt.Color(0, 0, 0),
                    new java.awt.Color(0, 0, 255),
                    true,
                    true,
                    new datechooser.view.appearance.swing.ButtonPainter()),
                new datechooser.view.appearance.swing.SwingCellAppearance(new java.awt.Font("Tahoma", java.awt.Font.PLAIN, 11),
                    new java.awt.Color(0, 0, 255),
                    new java.awt.Color(0, 0, 255),
                    false,
                    true,
                    new datechooser.view.appearance.swing.ButtonPainter()),
                new datechooser.view.appearance.swing.SwingCellAppearance(new java.awt.Font("Tahoma", java.awt.Font.PLAIN, 11),
                    new java.awt.Color(128, 128, 128),
                    new java.awt.Color(0, 0, 255),
                    false,
                    true,
                    new datechooser.view.appearance.swing.LabelPainter()),
                new datechooser.view.appearance.swing.SwingCellAppearance(new java.awt.Font("Tahoma", java.awt.Font.PLAIN, 11),
                    new java.awt.Color(0, 0, 0),
                    new java.awt.Color(0, 0, 255),
                    false,
                    true,
                    new datechooser.view.appearance.swing.LabelPainter()),
                new datechooser.view.appearance.swing.SwingCellAppearance(new java.awt.Font("Tahoma", java.awt.Font.PLAIN, 11),
                    new java.awt.Color(0, 0, 0),
                    new java.awt.Color(255, 0, 0),
                    false,
                    false,
                    new datechooser.view.appearance.swing.ButtonPainter()),
                (datechooser.view.BackRenderer)null,
                false,
                true)));
    calendario.setNothingAllowed(false);
    calendario.setWeekStyle(datechooser.view.WeekDaysStyle.FULL);
    calendario.setNavigateFont(new java.awt.Font("Tahoma", java.awt.Font.PLAIN, 11));
    calendario.setBehavior(datechooser.model.multiple.MultyModelBehavior.SELECT_PERIOD);
    calendario.addSelectionChangedListener(new datechooser.events.SelectionChangedListener() {
        public void onSelectionChange(datechooser.events.SelectionChangedEvent evt) {
            calendarioOnSelectionChange(evt);
        }
    });

    tablaAgenda.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {

        },
        new String [] {
            "Hora", "Paciente", "Dni", "Telefono", "Obra Social", "Otra descripcion", "Asistencia"
        }
    ) {
        Class[] types = new Class [] {
            java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Boolean.class
        };
        boolean[] canEdit = new boolean [] {
            false, true, true, true, true, true, true
        };

        public Class getColumnClass(int columnIndex) {
            return types [columnIndex];
        }

        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return canEdit [columnIndex];
        }
    });
    tablaAgenda.setColumnSelectionAllowed(true);
    tablaAgenda.setEnabled(false);
    tablaAgenda.setSelectionBackground(new java.awt.Color(255, 204, 204));
    tablaAgenda.setSelectionForeground(new java.awt.Color(0, 0, 0));
    tablaAgenda.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            tablaAgendaMouseClicked(evt);
        }
    });
    tablaAgenda.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
        public void mouseMoved(java.awt.event.MouseEvent evt) {
            tablaAgendaMouseMoved(evt);
        }
    });
    tablaAgenda.addContainerListener(new java.awt.event.ContainerAdapter() {
        public void componentAdded(java.awt.event.ContainerEvent evt) {
            tablaAgendaComponentAdded(evt);
        }
    });
    jScrollPane1.setViewportView(tablaAgenda);
    tablaAgenda.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

    jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/turnibook/images/icono_impresora.jpg"))); // NOI18N
    jButton1.setText("Imprimir");
    jButton1.setMaximumSize(new java.awt.Dimension(93, 27));
    jButton1.setMinimumSize(new java.awt.Dimension(93, 27));
    jButton1.setPreferredSize(new java.awt.Dimension(89, 25));
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });

    jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/turnibook/images/icono_carta3.jpg"))); // NOI18N
    jButton2.setText("Enviar");
    jButton2.setMaximumSize(new java.awt.Dimension(93, 27));
    jButton2.setMinimumSize(new java.awt.Dimension(93, 27));
    jButton2.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton2ActionPerformed(evt);
        }
    });

    panel1.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.background"));

    Dni.setText("Dni");
    Dni.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            DniMouseClicked(evt);
        }
    });
    Dni.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            DniActionPerformed(evt);
        }
    });

    Nombre.setText("Nombre");
    Nombre.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            NombreMouseClicked(evt);
        }
    });
    Nombre.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            NombreActionPerformed(evt);
        }
    });

    obraSocial.setText("O.S");
    obraSocial.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            obraSocialMouseClicked(evt);
        }
    });
    obraSocial.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            obraSocialActionPerformed(evt);
        }
    });

    buscar1.setEnabled(false);
    buscar1.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyPressed(java.awt.event.KeyEvent evt) {
            buscar1KeyPressed(evt);
        }
        public void keyTyped(java.awt.event.KeyEvent evt) {
            buscar1KeyTyped(evt);
        }
    });

    buscar2.setEnabled(false);
    buscar2.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyPressed(java.awt.event.KeyEvent evt) {
            buscar2KeyPressed(evt);
        }
        public void keyTyped(java.awt.event.KeyEvent evt) {
            buscar2KeyTyped(evt);
        }
    });

    buscar3.setEnabled(false);
    buscar3.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyTyped(java.awt.event.KeyEvent evt) {
            buscar3KeyTyped(evt);
        }
    });

    botonBuscar.setActionCommand("buscar");
    botonBuscar.setEnabled(false);
    botonBuscar.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
    botonBuscar.setLabel("Buscar");
    botonBuscar.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            botonBuscarActionPerformed(evt);
        }
    });

    lista.setBackground(new java.awt.Color(240, 240, 240));
    lista.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
    lista.setForeground(new java.awt.Color(102, 102, 255));
    jScrollPane2.setViewportView(lista);

    proximo.setText("Proximo Turno");
    proximo.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            proximoActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout panel1Layout = new javax.swing.GroupLayout(panel1);
    panel1.setLayout(panel1Layout);
    panel1Layout.setHorizontalGroup(
        panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(panel1Layout.createSequentialGroup()
            .addGap(17, 17, 17)
            .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 401, Short.MAX_VALUE)
                .addGroup(panel1Layout.createSequentialGroup()
                    .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(obraSocial, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(Nombre, javax.swing.GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE)
                        .addComponent(Dni, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(buscar1)
                        .addComponent(buscar2)
                        .addComponent(buscar3))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(botonBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addComponent(proximo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addContainerGap())
    );
    panel1Layout.setVerticalGroup(
        panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(panel1Layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(botonBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(panel1Layout.createSequentialGroup()
                    .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(buscar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(Dni))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(buscar2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(Nombre))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(obraSocial)
                        .addComponent(buscar3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
            .addGap(18, 18, 18)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addComponent(proximo, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(38, Short.MAX_VALUE))
    );

    notas.setBackground(new java.awt.Color(240, 240, 240));
    notas.setColumns(20);
    notas.setFont(new java.awt.Font("Segoe Print", 0, 14)); // NOI18N
    notas.setForeground(new java.awt.Color(255, 51, 102));
    notas.setLineWrap(true);
    notas.setRows(5);
    notas.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Notas", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Segoe Print", 0, 10), new java.awt.Color(102, 0, 153))); // NOI18N
    notas.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            notasMouseClicked(evt);
        }
        public void mousePressed(java.awt.event.MouseEvent evt) {
            notasMousePressed(evt);
        }
    });
    notas.addContainerListener(new java.awt.event.ContainerAdapter() {
        public void componentAdded(java.awt.event.ContainerEvent evt) {
            notasComponentAdded(evt);
        }
    });
    notas.addFocusListener(new java.awt.event.FocusAdapter() {
        public void focusGained(java.awt.event.FocusEvent evt) {
            notasFocusGained(evt);
        }
    });
    notas.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyPressed(java.awt.event.KeyEvent evt) {
            notasKeyPressed(evt);
        }
        public void keyReleased(java.awt.event.KeyEvent evt) {
            notasKeyReleased(evt);
        }
    });
    jScrollPane3.setViewportView(notas);

    gnota.setText("Guardar");
    gnota.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            gnotaActionPerformed(evt);
        }
    });

    bnota.setText("Borrar");
    bnota.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            bnotaActionPerformed(evt);
        }
    });

    aviso.setEditable(false);
    aviso.setBorder(null);
    aviso.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            avisoActionPerformed(evt);
        }
    });

    Sobreturno.setIcon(new javax.swing.ImageIcon(getClass().getResource("/turnibook/images/ico_agregar.gif"))); // NOI18N
    Sobreturno.setText("Agregar Sobreturno");
    Sobreturno.setMaximumSize(new java.awt.Dimension(93, 27));
    Sobreturno.setMinimumSize(new java.awt.Dimension(93, 27));
    Sobreturno.setPreferredSize(new java.awt.Dimension(89, 25));
    Sobreturno.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            SobreturnoActionPerformed(evt);
        }
    });

    bcancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/turnibook/images/eliminar.gif"))); // NOI18N
    bcancelar.setText("Cancelar");
    bcancelar.setActionCommand("");
    bcancelar.setMaximumSize(new java.awt.Dimension(93, 27));
    bcancelar.setMinimumSize(new java.awt.Dimension(93, 27));
    bcancelar.setPreferredSize(new java.awt.Dimension(89, 25));
    bcancelar.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            bcancelarMouseClicked(evt);
        }
    });
    bcancelar.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            bcancelarActionPerformed(evt);
        }
    });

    jMenu3.setText("Ayuda");

    jMenuItem1.setText("Acerca de...");
    jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jMenuItem1ActionPerformed(evt);
        }
    });
    jMenu3.add(jMenuItem1);
    jMenu3.add(jSeparator1);

    jMenuItem3.setText("Salir");
    jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jMenuItem3ActionPerformed(evt);
        }
    });
    jMenu3.add(jMenuItem3);

    jMenuBar1.add(jMenu3);

    setJMenuBar(jMenuBar1);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(Sobreturno, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(bmodificar, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(beliminar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(bguardar, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(bcancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addComponent(calendario, javax.swing.GroupLayout.PREFERRED_SIZE, 361, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(62, 62, 62)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(gnota, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(bnota, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                    .addGap(51, 51, 51)
                    .addComponent(panel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(10, 10, 10))
                .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(aviso, javax.swing.GroupLayout.PREFERRED_SIZE, 741, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 0, Short.MAX_VALUE)))
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(calendario, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createSequentialGroup()
                    .addGap(11, 11, 11)
                    .addComponent(jScrollPane3)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(gnota)
                        .addComponent(bnota)))
                .addComponent(panel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 235, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Sobreturno, javax.swing.GroupLayout.DEFAULT_SIZE, 27, Short.MAX_VALUE)
                    .addComponent(bmodificar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(beliminar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(bguardar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bcancelar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 27, Short.MAX_VALUE)
                    .addComponent(jButton2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
            .addGap(9, 9, 9)
            .addComponent(aviso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(6, 6, 6))
    );

    getAccessibleContext().setAccessibleDescription("");

    pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tablaAgendaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaAgendaMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_tablaAgendaMouseClicked

    private void bmodificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bmodificarActionPerformed
        if (tablaAgenda.getRowCount() > 0) {
            tablaAgenda.setEnabled(true);
            bcancelar.setEnabled(true);
            beliminar.setEnabled(true);
            bguardar.setEnabled(true);
            bmodificar.setEnabled(false);
            calendario.setEnabled(false);
            jButton1.setEnabled(false);
            jButton2.setEnabled(false);
            panel1.setEnabled(false);
            botonBuscar.setEnabled(false);
            notas.setEnabled(false);
            proximo.setEnabled(false);
            gnota.setEnabled(false);
            bnota.setEnabled(false);
            Sobreturno.setEnabled(false);
            Dni.setEnabled(false);
            Nombre.setEnabled(false);
            obraSocial.setEnabled(false);
        }
    }//GEN-LAST:event_bmodificarActionPerformed

    private String fechaFormateada(String fecha) {
        String resultado = "";
        resultado = String.valueOf(fecha.charAt(0)) + String.valueOf(fecha.charAt(1));
        resultado = "/" + resultado;
        resultado = String.valueOf(fecha.charAt(2)) + String.valueOf(fecha.charAt(3)) + resultado;
        resultado = "/" + resultado;
        if (fecha.length() == 5) {
            resultado = String.valueOf(fecha.charAt(4)) + resultado;
        } else {
            resultado = String.valueOf(fecha.charAt(4)) + String.valueOf(fecha.charAt(5)) + resultado;
        }

        return resultado;
    }

    private String nuevaFecha(Date fecha) {
        String patron = "yyyy-MM-dd";
        SimpleDateFormat formato = new SimpleDateFormat(patron);
        String result = formato.format(fecha);
        return result;
//        String[] actual = fecha.split("/");
//        actual[0] = actual[0].replaceAll("\\[", "");
//        actual[2] = actual[2].replaceAll("\\]", "");
//        if (Integer.valueOf(actual[0]) < 10) {
//            actual[0] = "0" + String.valueOf(Integer.valueOf(actual[0]));
//        } else {
//            return actual[2] + actual[1] + actual[0];
//        }
//        return actual[2] + actual[1] + actual[0];
    }

    /**
     * Elimino una fila seleccionada.
     *
     * @param evt
     */
    private void beliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_beliminarActionPerformed
        if (bmodificar.isEnabled() == false) {
            int fila;
            fila = tablaAgenda.getSelectedRow();
            Operaciones o = new Operaciones();
            //hora y dia le paso para que borre        
            String fecha = nuevaFecha(calendario.getSelectedDate().getTime());
            o.borrarTurno(tablaAgenda.getValueAt(fila, HORA).toString(), fecha, id_profesional);
            int max = tablaAgenda.getColumnCount();
            for (int i = 1; i < max; i++) {
                tablaAgenda.setValueAt(null, fila, i);
            }

            tablaAgenda.setEnabled(false);
            
            tablaAgenda.clearSelection();
            tablaAgenda.setEnabled(false);
             aviso.setText("Los datos se eliminaron correctamente");
            bmodificar.setEnabled(true);
            calendario.setEnabled(true);
            jButton1.setEnabled(true);
            jButton2.setEnabled(true);
            panel1.setEnabled(true);
            botonBuscar.setEnabled(true);
            proximo.setEnabled(true);
            gnota.setEnabled(true);
            bnota.setEnabled(true);
            Sobreturno.setEnabled(true);
            Dni.setEnabled(true);
            Nombre.setEnabled(true);
            obraSocial.setEnabled(true);
            beliminar.setEnabled(false);
            bguardar.setEnabled(false);
            notas.setEnabled(true);
            bcancelar.setEnabled(false);
            
           
        }
    }//GEN-LAST:event_beliminarActionPerformed
    /**
     * Guarda los elementos de una tabla
     *
     * @param evt
     */
    private void bguardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bguardarActionPerformed
try{
        if (bmodificar.isEnabled() == false) {
            if (tablaAgenda.isCellEditable(tablaAgenda.getSelectedRow(), tablaAgenda.getSelectedColumn())) {
                tablaAgenda.editCellAt(0, 0);
            }

            Operaciones o = new Operaciones();
            int fila = 0;
            int max = tablaAgenda.getRowCount();

            String actual = nuevaFecha(calendario.getSelectedDate().getTime());
            while (fila < max) {
                if (tablaAgenda.getValueAt(fila, NOMBRE) != null) {
                    if (!o.existeTurno(actual, tablaAgenda.getValueAt(fila, HORA).toString())) {
                        String sql = "insert into turno(documento,telefono,asistencia,descripcion,os,nombre,hora,fecha,id_profesional) values ('" + tablaAgenda.getValueAt(fila, DOC) + "','" + tablaAgenda.getValueAt(fila, TEL) + "','" + tablaAgenda.getValueAt(fila, ASISTENCIA) + "','" + tablaAgenda.getValueAt(fila, DESCRIPCION) + "','" + tablaAgenda.getValueAt(fila, OS) + "','" + tablaAgenda.getValueAt(fila, NOMBRE) + "','" + tablaAgenda.getValueAt(fila, HORA) + "','" + actual + "','" + id_profesional + "')";
                        o.insertar(sql, aviso);
                    } else {
                        String sql = "update turno set ";
                        String documento = (String) tablaAgenda.getValueAt(fila, DOC);
                        String telefono = (String) tablaAgenda.getValueAt(fila, TEL);
                        Boolean asistencia = (Boolean) tablaAgenda.getValueAt(fila, ASISTENCIA);
                        String descripcion = (String) tablaAgenda.getValueAt(fila, DESCRIPCION);
                        String os = (String) tablaAgenda.getValueAt(fila, OS);
                        String nombre = (String)tablaAgenda.getValueAt(fila, NOMBRE);
                        boolean entro = false;                        
                        if (documento!=null){
                            sql = sql + "documento='" + tablaAgenda.getValueAt(fila, DOC)+"'";
                            entro=true;
                        }
                        if (telefono!=null){
                            if (!entro){
                                sql = sql + "telefono='" + tablaAgenda.getValueAt(fila, TEL)+"'";
                            }else{
                                sql = sql + ",telefono='" + tablaAgenda.getValueAt(fila, TEL)+"'";
                            }
                            entro=true;
                        }
                        if (asistencia != null && asistencia){
                           if (!entro){
                            sql = sql + "asistencia='" + tablaAgenda.getValueAt(fila, ASISTENCIA)+"'";
                           }else{
                               sql = sql + ", asistencia='" + tablaAgenda.getValueAt(fila, ASISTENCIA)+"'";
                           }
                           
                            entro=true;
                        }
                        if(descripcion!=null){
                            if (!entro){
                                sql = sql + "descripcion='" + tablaAgenda.getValueAt(fila, DESCRIPCION)+"'";
                            }else{
                                sql = sql + ", descripcion='" + tablaAgenda.getValueAt(fila, DESCRIPCION)+"'";
                            }
                            
                            entro=true;
                        }                        
                        if (os!=null){
                            if (!entro){
                                sql = sql + "os='" + tablaAgenda.getValueAt(fila, OS)+"'";
                            }else{
                                sql = sql + ", os='" + tablaAgenda.getValueAt(fila, OS)+"'";
                            }
                            entro=true;
                        }
                        if (nombre!=null){
                            if (!entro){
                                sql = sql + "nombre='" + tablaAgenda.getValueAt(fila, NOMBRE)+"'";
                            }else{
                                sql = sql + ", nombre='" + tablaAgenda.getValueAt(fila, NOMBRE)+"'";
                            }
                            
                            entro=true;
                        }
                        if (entro){
                            sql = sql + "where hora='" + tablaAgenda.getValueAt(fila, HORA);
                            sql = sql + "' and fecha='" + actual;
                            sql = sql + "' and id_profesional='" + id_profesional + "'";

                            if (o.updateTurno(sql)) {
                                aviso.setText("Los datos se actualizaron correctamente");
                            } else {
                                aviso.setText("Los datos no fueron actualizados");
                            }
                        }
                    }
                }
                fila++;
            }

            tablaAgenda.clearSelection();
            tablaAgenda.setEnabled(false);
            bmodificar.setEnabled(true);
            calendario.setEnabled(true);
            jButton1.setEnabled(true);
            jButton2.setEnabled(true);
            panel1.setEnabled(true);
            botonBuscar.setEnabled(true);
            proximo.setEnabled(true);
            gnota.setEnabled(true);
            bnota.setEnabled(true);
            Sobreturno.setEnabled(true);
            Dni.setEnabled(true);
            Nombre.setEnabled(true);
            obraSocial.setEnabled(true);
            beliminar.setEnabled(false);
            bguardar.setEnabled(false);
            notas.setEnabled(true);
            bcancelar.setEnabled(false);
        }

        }catch (Exception ex){
           aviso.setText("Ocurrio un ERROR."); 
        }
    }//GEN-LAST:event_bguardarActionPerformed

    private void bmodificarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_bmodificarMouseClicked
    }//GEN-LAST:event_bmodificarMouseClicked

    private void bguardarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_bguardarMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_bguardarMouseClicked

    private void beliminarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_beliminarMouseClicked
    }//GEN-LAST:event_beliminarMouseClicked

    private void tablaAgendaMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaAgendaMouseMoved
        // TODO add your handling code here:
    }//GEN-LAST:event_tablaAgendaMouseMoved

    private void tablaAgendaComponentAdded(java.awt.event.ContainerEvent evt) {//GEN-FIRST:event_tablaAgendaComponentAdded
    }//GEN-LAST:event_tablaAgendaComponentAdded

    private void calendarioOnSelectionChange(datechooser.events.SelectionChangedEvent evt) {//GEN-FIRST:event_calendarioOnSelectionChange
        generarTabla();
        tablaAgenda.setEnabled(false);
    }//GEN-LAST:event_calendarioOnSelectionChange
    
    private String getNombreDay(int nombredia){
        return dia.get(nombredia);
    }
    
    /**
     * Este metodo generar la tabla con la informacion de la base de datos
     */
    
    private void generarTabla() {
        Operaciones o = new Operaciones();
        
        // Con esto hago la consulta de cual horario pertenece al profesional con ese id_horario en el dia de la semana 
        String intervalo = o.getIntervalo(dia.get(calendario.getSelectedDate().getTime().getDay()), id_horario);
        //Separo las variables inicio fin intervalo
        if (!"".equals(intervalo) && !"fin".equals(intervalo)) {
            String[] res = intervalo.split("-");

            String actual = nuevaFecha(calendario.getSelectedDate().getTime());
//            Date select;
//            select = calendario.getSelectedDate().getTime();
            tiempo = Integer.valueOf(res[2]);
            o.generarHorario(res[0], res[1], Integer.valueOf(res[2]), tablaAgenda, id_profesional, actual);
            Sobreturno.setEnabled(true);
        }else{
            o.generarHorario("0:0", "0:0", 0, tablaAgenda, id_profesional, "0");
        }
    }

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        generarTabla();
        tablaAgenda.setEnabled(false);
        Archivo a = new Archivo();
        String nota = a.leerTxt(id_profesional);
        notas.append(nota);

    }//GEN-LAST:event_formWindowOpened
    /**
     * Metodo para buscar el por nombre, obra social y documento
     *
     * @param evt
     */
    private void botonBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonBuscarActionPerformed
        try {
            Operaciones o = new Operaciones();
            ResultSet r = null;
            
            Date fechaDate = new Date();
            String nuevaFecha = nuevaFecha(fechaDate);
            String sql = "select nombre,fecha,hora  from turno where fecha >=" + nuevaFecha;
            //busco por dni
            if (Dni.isSelected()) {
                sql = sql + " and documento like '" + buscar1.getText() + "%'";
            }


            if (Nombre.isSelected() && Nombre.isSelected()) {
                sql = sql + " and nombre like '" + buscar2.getText() + "%'";
            }

            if ((obraSocial.isSelected() || Nombre.isSelected()) && obraSocial.isSelected()) {
                sql = sql + " and os like '" + buscar3.getText() + "%'";
            }


            r = o.consultar(sql);
            DefaultListModel list = new DefaultListModel();

            while (r.next()) {
                list.addElement("      " + r.getString("nombre") + "     " + r.getString("fecha") + "     " + r.getString("hora"));
            }
            lista.setModel(list);
            r.close();
            aviso.setText("La busqueda se realizo con exito");

            buscar1.setText("");
            buscar2.setText("");
            buscar3.setText("");
            buscar1.enable(false);
            buscar2.enable(false);
            buscar3.enable(false);
            Dni.setSelected(false);
            Nombre.setSelected(false);
            obraSocial.setSelected(false);
            o.closeDB();
        } catch (SQLException ex) {
            Logger.getLogger(Agenda.class.getName()).log(Level.SEVERE, null, ex);
        }
        botonBuscar.setEnabled(false);
        

    }//GEN-LAST:event_botonBuscarActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        String nombre = calendario.getSelection().toString().replace("/", "-");
        CrearPdf pdf = new CrearPdf(nombre, tablaAgenda);

    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed

        if (!email.equals("")) {

            String destino = email;
            String asunto = "Turnos que corresponden al dia: " + calendario.getSelection().toString();
            String contenido = "<table> <tr><th>Horario</th> <th>Nombre </th> <th>Documento</th><th> Telefono </th><th>Obra Social </th></tr>\n";
            int fila = 0;
            int max = tablaAgenda.getRowCount();
            while (fila < max) {
                if (tablaAgenda.getValueAt(fila, NOMBRE) != null) {
                    contenido = contenido + "<tr><td>" + tablaAgenda.getValueAt(fila, HORA) + "</td> <td>" + tablaAgenda.getValueAt(fila, NOMBRE) + "</td> <td>" + tablaAgenda.getValueAt(fila, DOC) + "</td> ";
                    if (tablaAgenda.getValueAt(fila, TEL) !=null)
                        contenido = contenido +"<td>"+ tablaAgenda.getValueAt(fila, TEL) + "</td>";
                    else
                        contenido = contenido +"<td></td>";
                    if (tablaAgenda.getValueAt(fila, OS) !=null)
                        contenido = contenido + tablaAgenda.getValueAt(fila, OS);
                    else
                        contenido = contenido +"<td></td>";
                    contenido = contenido  + "</tr>\n";
                }
                fila++;
            }
            contenido = contenido + "</table>";
            EnviadorMail email = new EnviadorMail(destino, asunto, contenido);
            aviso.setText("El mail ha sido enviado con exito");

        } else {
            JOptionPane.showMessageDialog(null, "No se encuentra especificado el mail del profesional");
        }

    }//GEN-LAST:event_jButton2ActionPerformed

    private void DniMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_DniMouseClicked
    }//GEN-LAST:event_DniMouseClicked

    private void NombreMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_NombreMouseClicked
    }//GEN-LAST:event_NombreMouseClicked

    private void obraSocialMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_obraSocialMouseClicked
    }//GEN-LAST:event_obraSocialMouseClicked

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        Acerca u = new Acerca();
        u.setLocationRelativeTo(null);
        u.setVisible(true);
        u.pack();
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        System.exit(0);
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void DniActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DniActionPerformed
        if (Dni.isSelected()) {
            buscar1.enable(true);
            buscar1.grabFocus();
        } else {
            buscar1.setText("");
            buscar1.enable(false);
            botonBuscar.enable(false);
        }

    }//GEN-LAST:event_DniActionPerformed

    private void NombreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NombreActionPerformed
        if (Nombre.isSelected()) {
            buscar2.enable(true);
            buscar2.grabFocus();
        } else {
            buscar2.setText("");
            buscar2.enable(false);
            botonBuscar.enable(false);
        }
    }//GEN-LAST:event_NombreActionPerformed

    private void obraSocialActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_obraSocialActionPerformed
        if (obraSocial.isSelected()) {
            buscar3.enable(true);
            buscar3.grabFocus();
        } else {
            buscar3.setText("");
            buscar3.enable(false);
            botonBuscar.enable(false);
        }

    }//GEN-LAST:event_obraSocialActionPerformed

    private void buscar1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_buscar1KeyPressed
    }//GEN-LAST:event_buscar1KeyPressed

    private void buscar1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_buscar1KeyTyped
        if (buscar1.getText().length() > 0) {
            botonBuscar.setEnabled(true);

        } else {
            botonBuscar.setEnabled(false);
        }
    }//GEN-LAST:event_buscar1KeyTyped

    private void buscar2KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_buscar2KeyTyped
        // TODO add your handling code here:
    }//GEN-LAST:event_buscar2KeyTyped

    private void buscar2KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_buscar2KeyPressed
        if (buscar2.getText().length() > 0) {
            botonBuscar.setEnabled(true);

        } else {
            botonBuscar.setEnabled(false);
        }
    }//GEN-LAST:event_buscar2KeyPressed

    private void buscar3KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_buscar3KeyTyped
        if (buscar3.getText().length() > 0) {
            botonBuscar.setEnabled(true);

        } else {
            botonBuscar.setEnabled(false);
        }
    }//GEN-LAST:event_buscar3KeyTyped

    private void gnotaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_gnotaActionPerformed
        String n = notas.getText();
        Archivo a = new Archivo();
        a.crearTxt(n, id_profesional);
        aviso.setText("La nota se guardo correctamente");
    }//GEN-LAST:event_gnotaActionPerformed

    private void notasKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_notasKeyPressed
    }//GEN-LAST:event_notasKeyPressed

    private void notasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_notasMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_notasMouseClicked

    private void notasMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_notasMousePressed
        // TODO add your handling code here:
    }//GEN-LAST:event_notasMousePressed

    private void notasFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_notasFocusGained
        // TODO add your handling code here:
    }//GEN-LAST:event_notasFocusGained

    private void notasComponentAdded(java.awt.event.ContainerEvent evt) {//GEN-FIRST:event_notasComponentAdded
        // TODO add your handling code here:
    }//GEN-LAST:event_notasComponentAdded

    private void notasKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_notasKeyReleased
        // TODO add your handling code here:
    }//GEN-LAST:event_notasKeyReleased

    private void bnotaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnotaActionPerformed

        int confirmado = JOptionPane.showConfirmDialog(null, "¿Esta seguro que desea borrar la nota?", "Borrar", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE);

        if (JOptionPane.YES_OPTION == confirmado) {
            Archivo a = new Archivo();
            a.crearTxt("", id_profesional);
            notas.setText("");
        }

    }//GEN-LAST:event_bnotaActionPerformed

    private void avisoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_avisoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_avisoActionPerformed

    private void formMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseMoved
        aviso.setText("");
    }//GEN-LAST:event_formMouseMoved

    private void SobreturnoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SobreturnoActionPerformed
        DefaultTableModel model = (DefaultTableModel) tablaAgenda.getModel();
        String t = (String) tablaAgenda.getValueAt(tablaAgenda.getRowCount() - 1, 0);
        String[] resultado = t.split(":");
        int min = Integer.valueOf(resultado[1]);
        int hora = Integer.valueOf(resultado[0]);
        min = min + tiempo;
        if (min >= 60) {
            min = min - 60;
            hora = hora + 1;
        }
        if (min == 0) {
            model.addRow(new Object[]{hora + ":00"});
        } else {
            model.addRow(new Object[]{hora + ":" + min});
        }
        
        
    }//GEN-LAST:event_SobreturnoActionPerformed

    private void proximoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_proximoActionPerformed
        int i = 0;
        boolean hayTurno = false;
        Date hoy = new Date();
        Date actual = null;
        Operaciones o = new Operaciones();
        while (!hayTurno) {
            actual = this.calendario.getModel().getCellDate(0, i).getTime();
            if (actual.compareTo(hoy) > 0) {
              
                    if (o.hayTurno(nuevaFecha(actual), getNombreDay(this.calendario.getModel().getCellDate(0, i).getTime().getDay()),id_horario,id_profesional)) {
                        hayTurno = true;
                    }
              
            }

            i++;

    }//GEN-LAST:event_proximoActionPerformed
        DefaultListModel list = new DefaultListModel();
        list.addElement("Proximo turno " + nuevaFecha(actual));
        Calendar cal=Calendar.getInstance();
        cal.setTime(actual);
        this.calendario.setSelectedDate(cal);
        lista.setModel(list);
    }
    private void bcancelarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_bcancelarMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_bcancelarMouseClicked

    private void bcancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bcancelarActionPerformed
        tablaAgenda.setEnabled(false);
        beliminar.setEnabled(false);
        bguardar.setEnabled(false);
        bmodificar.setEnabled(true);
        calendario.setEnabled(true);
        jButton1.setEnabled(true);
        jButton2.setEnabled(true);
        panel1.setEnabled(true);
        botonBuscar.setEnabled(true);
        notas.setEnabled(true);
        proximo.setEnabled(true);
        gnota.setEnabled(true);
        bnota.setEnabled(true);
        Sobreturno.setEnabled(true);
        Dni.setEnabled(true);
        Nombre.setEnabled(true);
        obraSocial.setEnabled(true);
        bcancelar.setEnabled(false);
    }//GEN-LAST:event_bcancelarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Agenda.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Agenda.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Agenda.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Agenda.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Agenda().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox Dni;
    private javax.swing.JCheckBox Nombre;
    private javax.swing.JButton Sobreturno;
    private javax.swing.JTextField aviso;
    private javax.swing.JButton bcancelar;
    private javax.swing.JButton beliminar;
    private javax.swing.JButton bguardar;
    private javax.swing.JButton bmodificar;
    private javax.swing.JButton bnota;
    private java.awt.Button botonBuscar;
    private javax.swing.JTextField buscar1;
    private javax.swing.JTextField buscar2;
    private javax.swing.JTextField buscar3;
    private javax.swing.ButtonGroup buttonGroup1;
    private datechooser.beans.DateChooserPanel calendario;
    private datechooser.beans.DateChooserDialog dateChooserDialog1;
    private datechooser.beans.DateChooserDialog dateChooserDialog2;
    private datechooser.beans.DateChooserDialog dateChooserDialog3;
    private datechooser.beans.DateChooserPanel dateChooserPanel1;
    private javax.swing.JButton gnota;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JList lista;
    private javax.swing.JTextArea notas;
    private javax.swing.JCheckBox obraSocial;
    private java.awt.Panel panel1;
    private javax.swing.JButton proximo;
    private javax.swing.JTable tablaAgenda;
    // End of variables declaration//GEN-END:variables
}
